package com.lisx.module_memo.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.fenghuajueli.lib_data.entity.db.MemoDbEntity;
import com.lisx.module_memo.databinding.ItemMemoListBinding;
import com.lisx.module_memo.utils.TimeUtils;

import java.util.ArrayList;
import java.util.List;

public class MemoListAdapter extends RecyclerView.Adapter<MemoListAdapter.MyViewHolder> {

    List<MemoDbEntity> list = new ArrayList<>();

    public void setData(List<MemoDbEntity> list) {
        List<MemoDbEntity> zdList = new ArrayList<>();
        //置顶
        for (int i = 0; i < list.size(); i++) {
            MemoDbEntity memoDbEntity = list.get(i);
            if (memoDbEntity.getIsTop()) {
                list.remove(i);
                i--;
                zdList.add(memoDbEntity);
            }
        }
        this.list.clear();
        this.list.addAll(zdList);
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemMemoListBinding binding = ItemMemoListBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new MyViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        MemoDbEntity memoDbEntity = list.get(position);
        holder.binding.tvContent.setText(memoDbEntity.getMsg());
        holder.binding.tvTitle.setText(memoDbEntity.getTitle());
        holder.binding.tvTime.setText(TimeUtils.parseHMTimeLong(memoDbEntity.getRecord_time() / 1000));
        if (memoDbEntity.getIsTop()) {
            holder.binding.tvZhiding.setVisibility(View.VISIBLE);
        } else {
            holder.binding.tvZhiding.setVisibility(View.GONE);
        }
        holder.binding.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onModifyMemo(memoDbEntity,1);
                }
            }
        });
        holder.binding.ivEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onEdit(memoDbEntity);
                }
            }
        });
        holder.binding.ivZhiding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onModifyMemo(memoDbEntity,2);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private final ItemMemoListBinding binding;

        public MyViewHolder(@NonNull ItemMemoListBinding itemView) {
            super(itemView.getRoot());
            binding = itemView;
        }
    }

    public interface OnItemClickListener {
        void onEdit(MemoDbEntity memoDbEntity);

        void onModifyMemo(MemoDbEntity memoDbEntity,int modify);
    }

    OnItemClickListener listener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

}
